package com.cloud.school.bus.teacherhelper.modules.home.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.support.jhf.touchimageview.BasePagerAdapter;
import com.android.support.jhf.touchimageview.GalleryViewPager;
import com.android.support.jhf.touchimageview.TouchImageView;
import com.android.support.jhf.universalimageloader.core.DisplayImageOptions;
import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.assist.FailReason;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.ImageUtil;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Picture;

public class BigPictureAdapter extends BasePagerAdapter {

	private List<Picture> mPictures;
	private Context mContext;
	private TeacherHelperApplication mApplication;
	private DisplayImageOptions mDisplayImageOptions;

	public BigPictureAdapter(Context context, List<Picture> pictures) {
		super(context, null);
		mPictures = pictures;
		mContext = context;
		mApplication = (TeacherHelperApplication) context
				.getApplicationContext();

	}

	@Override
	public int getCount() {
		return mPictures.size();

	}

	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		super.setPrimaryItem(container, position, object);
		((GalleryViewPager) container).mCurrentView = (TouchImageView) ((View) object)
				.findViewById(R.id.touchImageView);
	}

	@Override
	public Object instantiateItem(ViewGroup collection, int position) {
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.adapter_big_picture, null);

		Picture picture = mPictures.get(position);

		if (picture.isPictureHttp()) {
			mDisplayImageOptions = mApplication.mCacheDisplayImageOptions;
		} else {
			mDisplayImageOptions = mApplication.mNoCacheDisplayImageOptions;
		}

		final TouchImageView touchImageView = (TouchImageView) view
				.findViewById(R.id.touchImageView);
		final ViewGroup waitLayout = (ViewGroup) view
				.findViewById(R.id.waitLayout);
		waitLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});
		waitLayout.setVisibility(View.VISIBLE);
		touchImageView.setLayoutParams(new RelativeLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
		if (!TextUtils.isEmpty(mPictures.get(position).getPicturePath())) {
			ImageLoader.getInstance().displayImage(picture.getPicturePath(),
					touchImageView, mDisplayImageOptions,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingStarted(String imageUri, View view) {
							waitLayout.setVisibility(View.VISIBLE);
						}

						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {

							TouchImageView tempTouchImageView = (TouchImageView)view;
							if(imageUri.startsWith("http://")){
								tempTouchImageView.setImageBitmap(loadedImage);
							}else if(imageUri.startsWith("file:///")){
								ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, tempTouchImageView);
							}else {
								ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, tempTouchImageView);
							}
							waitLayout.setVisibility(View.GONE);
						}

						@Override
						public void onLoadingFailed(String imageUri, View view,
								FailReason failReason) {
						}
					});
		}
		collection.addView(view, 0);
		return view;
	}

}
